<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <GroupTabs
      ref="groupTabs1"
      :api="getTriggerGroupList"
      v-model:value="triggerGroup"
      @select="handleSelect"
    />
    <BasicTable @register="registerTable" class="overflow-hidden">
      <template #toolbar>
        <Authority :value="'trigger.add'">
          <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '立即执行',
                auth: 'trigger.run',
                onClick: handleRun.bind(null, record)
              },
              {
                label: '启动',
                auth: 'trigger.start',
                onClick: handleStart.bind(null, record)
              },
              {
                label: '停止',
                auth: 'trigger.stop',
                onClick: handleStop.bind(null, record)
              },
              {
                icon: 'clarity:note-edit-line',
                auth: 'trigger.edit',
                onClick: handleEdit.bind(null, record)
              },
              {
                icon: 'ant-design:delete-outlined',
                auth: 'trigger.del',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record)
                }
              }
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <EditDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { computed, ref, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Authority } from '/@/components/Authority';
  import { PageWrapper } from '/@/components/Page';
  import EditDrawer from './EditDrawer.vue';
  import GroupTabs from '/@/components/GroupTabs';
  import GroupTabsActionType from '/@/components/GroupTabs/typing';
  import { columns, searchFormSchema } from './data';
  import {
    getTriggerGroupList,
    getTriggerPage,
    removTrigger,
    startTrigger,
    stopTrigger,
    runTrigger
  } from '/@/api/job/trigger';

  const triggerGroup = ref('');
  const groupTabs1 = ref<GroupTabsActionType>();
  const handleSelect = async () => {
    await reload();
  };
  const { notification } = useMessage();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    title: '调度计划列表',
    api: getTriggerPage,
    immediate: false,
    searchInfo: computed(() => {
      return { triggerGroup };
    }),
    columns,
    formConfig: {
      labelWidth: 90,
      schemas: searchFormSchema
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action'
    }
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true
    });
  }

  const toKey = (record: Recordable) => {
    return { name: record.triggerName, group: record.triggerGroup };
  };
  const toKeys = (record: Recordable) => {
    return [toKey(record)];
  };
  async function handleDelete(record: Recordable) {
    await removTrigger(toKeys(record));
    await handleSuccess();
  }
  async function handleRun(record: Recordable) {
    await runTrigger(toKey(record));
    notification.success({ message: '执行成功' });
    await handleSuccess();
  }

  async function handleStart(record: Recordable) {
    await startTrigger(toKeys(record));
    notification.success({ message: '启动成功' });
    await handleSuccess();
  }
  async function handleStop(record: Recordable) {
    await stopTrigger(toKeys(record));
    notification.success({ message: '停止成功' });
    await handleSuccess();
  }
  const handleSuccess = async () => {
    await reload();
    await unref(groupTabs1)?.reload();
  };
</script>
